package top.makeprogress.taobaospider.config;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import top.makeprogress.taobaospider.util.LogPrintUtils;

public class ConfigProperties {
	private static Properties properties;
	private static String defaultProperties = "application.properties";
	public static void init(String... propertyPaths) {
		properties = new Properties();
		if (propertyPaths != null && propertyPaths.length != 0) {
			for (String propertyPath : propertyPaths) {
				load(propertyPath);
			}
		}else{
			load(defaultProperties);
		}

	}

	private static void load(String propertyPath) {
		try {
			InputStream inputStream = ConfigProperties.class.getClassLoader().getResourceAsStream(propertyPath);
			properties.load(inputStream);
		} catch (FileNotFoundException e) {
			LogPrintUtils.printErr(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			LogPrintUtils.printErr(e.getMessage());
			e.printStackTrace();
		}
	}

	private static String getProperty(String key) {
		if (properties == null) {
			init();
		}
		return properties.getProperty(key);
	}

	public static String getString(String key) {
		return getProperty(key);
	}

	public static String getString(String key, String defaultValue) {
		String property = getProperty(key);
		if (property == null || property.isEmpty()) {
			return defaultValue;
		}
		return property;
	}

	public static Boolean getBoolean(String key) {
		return Boolean.valueOf(getProperty(key));
	}

	public static Boolean getBoolean(String key, Boolean defaultValue) {
		String property = getProperty(key);
		if (property == null || property.isEmpty()) {
			return defaultValue;
		}
		return Boolean.valueOf(property);
	}

	public static Integer getInteger(String key) {
		return Integer.valueOf(getProperty(key));
	}

	public static Integer getInteger(String key, Integer defaultValue) {
		String property = getProperty(key);
		if (property == null || property.isEmpty()) {
			return defaultValue;
		}
		return Integer.valueOf(property);
	}

}
